<style lang="less" scoped>
@c-primary: #3399ff;
.panel {
  margin-bottom: 10px;
  &-title {
    display: flex;
    padding: 10px;
    border-bottom: 0.1px solid #eaeaea;
    background: white;
    &.first {
      margin-bottom: 20px;
    }

    &__label {
      flex: 1;
      font-size: 16px;
      align-items: center;
      &.name {
        font-size: 22px;
        display: flex;
      }
    }
    &__right {
      font-size: 18px;
    }
    &__status {
      border: 1px solid @c-primary;
      border-radius: 4px;
      padding: 2px 6px;
      color: @c-primary;
      font-size: 12px;
    }
  }
  &-content {
    margin-bottom: 10px;
    background: white;

    &__item {
      display: flex;
      padding: 10px 10px 0 10px;
      font-size: 14px;
      &-label {
        color: #666;
      }
      &-value {
        flex: 1;
        padding-left: 15px;
        color: #333;
      }
    }
  }
}
</style>

<template>
  <div>
    <div class="panel">
      <!-- title -->
      <div class="panel-title first" key="01">
        <div class="panel-title__label name">
          <text-to-avator :text="params.personName" />{{params.personName}}</div>
        <div class="panel-title__right">他关联的企业</div>
      </div>

      <template v-for="(item, index) in items" wx:key="unique">
        <!-- title -->
        <div class="panel-title" :key="'title'+index">
          <div class="panel-title__label avator">
            <image class="avator-img" v-if="item.ImageUrl" :src="item.ImageUrl" />
            <text-to-avator v-else :text="item.Name"></text-to-avator>
            <span class="primaryC" @click="itemClick(item.KeyNo)">{{item.Name}}</span>
          </div>
          <div class="panel-title__right">
            <status :status="item.Status" />
          </div>
        </div>
        <!-- content -->
        <div class="panel-content" :key="'content'+index">
          <div class="panel-content__item">
            <div class="panel-content__item-label">法定代表人</div>
            <div class="panel-content__item-value">{{item.OperName}}</div>
            <div class="panel-content__item-right"></div>
          </div>
          <div class="panel-content__item">
            <div class="panel-content__item-label">注册资本</div>
            <div class="panel-content__item-value">{{item.RegCap}}</div>
            <div class="panel-content__item-right"></div>
          </div>
          <div class="panel-content__item">
            <div class="panel-content__item-label">成立日期</div>
            <div class="panel-content__item-value">{{item.Date}}</div>
            <div class="panel-content__item-right"></div>
          </div>
        </div>
      </template>
    </div>
    <!-- search on loading -->
    <load v-if="loading" />

    <!-- 无数据 -->
    <div class="info" v-else-if="items.length===0||(items.length!==0&&totalPage===pageIndex)">无更多数据</div>
  </div>
</template>
<script>
import Status from '@/components/status';
import Load from '@/components/load';
import TextToAvator from '@/components/textToAvator';

export default {
  components: {
    Status,
    Load,
    TextToAvator
  },
  data() {
    return {
      params: {},
      items: [],
      TotalRecords: 0,
      totalPage: 0, // 总页数，默认为0
      pageIndex: 1 //当前页码，默认为1
    };
  },
  onLoad() {
    this.getInfos();
  },
  onUnload() {
    this.items = [];
  },
  onReachBottom() {
    // 上拉加载
    if (!this.loading && this.pageIndex < this.totalPage) {
      this.getInfos(this.pageIndex + 1);
    }
  },
  computed: {
    loading() {
      return this.$store.getters.loading;
    }
  },
  methods: {
    async getInfos(pageIndex = 1) {
      if (typeof pageIndex !== 'number') {
        pageIndex = 1;
      }
      if (pageIndex === 1) {
        this.items = [];
      }

      // 获取对外投资
      const url = '/xcx/getBossRelativeList';
      let Oper = {};
      let params = {};
      try {
        Oper = JSON.parse(this.$root.$mp.page.options.Oper);
        params = {
          personName: Oper.Name,
          personId: Oper.KeyNo,
          pageIndex
        };
        this.params = params;
      } catch (error) {}

      const { status, message, result } = await this.$http.get(url, params);

      if (status !== 200) {
        wx.showToast({
          title: message || '查询失败！',
          icon: 'none',
          duration: 2000
        });
        return;
      }

      //更新数据
      if (pageIndex === 1) {
        this.items = result.Result;
      } else {
        this.items = [...this.items, ...result.Result];
      }

      //更新分页信息
      try {
        this.pageIndex = pageIndex;
        const page = result.Paging.TotalRecords / result.Paging.PageSize;
        this.totalPage = Math.ceil(page);
        this.TotalRecords = result.Paging.TotalRecords;
      } catch (error) {
        this.totalPage = 0;
      }
    },
    itemClick(unique) {
      this.$router.push({
        path: '/pages/company/main',
        query: { unique }
      });
    }
  }
};
</script>
